//
// 识别类
// Created by heqian on 17-8-4.
//

#ifndef AUTOMAHJONG_IDENTIFY_H
#define AUTOMAHJONG_IDENTIFY_H

#include <vector>
#include "../image/Image.h"
#include "../image/EditImage.h"
#include "../image/MatchImage.h"

#include "Template.h"
#include "BoardInfo.h"

using namespace std;
using namespace img;

namespace mahjong {

    class Identify {
    public:
        Identify();

        virtual ~Identify();

        /**
        * 开始识别
        * @param image
        */
        void start(Prt<Image> &image);

        /**
         * 识别图像
         * @param img 源图像
         * @param items  模版组
         * @return 识别结果
         */
        MahjongEnum discernImage(Prt<Image> &img, vector<Prt<Item>> &items);

        /**
         * 识别缺少的麻将
         * @param rect
         * @return
         */
        MahjongEnum lackMahjong(Rect rect);

        /**
         * 识别庄家
         * @param rect
         * @return
         */
        bool isDealer(Rect rect);

        /**
         * 获得图像识别值
         * @param img
         * @param item
         * @return
         */
        MahjongEnum discernValue(Prt<Image> &img, Prt<Item> &item);

        /**
         * * 识别连续图像组
         * @param rect 识别区域
         * @param items　模板组
         * @param rect
         * @param items
         * @param width
         * @param height
         * @param offsetX
         * @param offestY
         */
        void
        discernBoards(Prt<vector<MahjongEnum>> board, Rect &rect, vector<Prt<Item>> &items,
                      int width, int height, Rect &templRect);

        /**
         * 获得最小匹配度
         * @return
         */
        double getMinMatching() const;

        /**
         * 设置最小匹配度
         * @param mMinMatching
         */
        void setMinMatching(double mMinMatching);

        Prt<BoardInfo> &getMBoardInfo();

        Prt<Template> mTemplate;
    protected:
        double mMinMatching;                    //最小匹配度 当匹配度大于这个值时
        Prt<Image> mImage;
        Prt<EditImage> mEdit;
        Prt<BoardInfo> mBoardInfo;

        int scanLeft(Prt<Image> img, Rect &rect, int y) const;

        int scanRight(Prt<Image> img, Rect &rect, int y) const;

        int scanTop(Prt<Image> img, Rect &rect, int x) const;

        int scanBottom(Prt<Image> img, Rect &rect, int x) const;

        void getUpBoard();

        void
        getOpenBoard(Prt<vector<MahjongEnum>> board, vector<Prt<Item>> &templ,
                     vector<ScanPos> &poss,
                     Size &size);

        void
        getBoard(Prt<vector<MahjongEnum>> board, vector<Prt<Item>> &templ, vector<ScanPos> &poss,
                 Size &size);

        void getDownBoard();

        void getTopBoard();

        void getMeBoard();
    };

}
#endif //AUTOMAHJONG_IDENTIFY_H
